﻿using System;
using System.Runtime.Serialization;
using StateManager;

namespace SMTestModel
{
    [DataContract]
    public class M1Cfg
    {
        [DataMember]
        public int Test = 2;
    }
    public class SMTestModel1 : IState<M1Cfg>
    {
        public void StateHandle(SInfo si)
        {
            var cfg = si.Config as M1Cfg;
            si.Log($"{cfg.Test}");
            SInfo m2 = si.Manager.GetSInfo("实例2") as SInfo;
            m2.Auto = true;
            int i = (int?)si.GetUserData("i") ?? 0;
            si.Log($"测试{i}");
            si.SetUserData("i", i + 1);
            si.RepeatState(200);
            //if (i == 3)
            //    si.Manager.Paused = true;

            if (i > 3)
            {
                SInfo newsi = new SInfo();
                newsi.Name = $"实例{i + 1000}";
                newsi.Type = "SMTestModel.SMTestModel1";
                newsi.Auto = true;
                si.Manager.AddSInfo(newsi);
                si.Manager.InitSObject(newsi);
                si.Manager.SaveConfigue();
            }
            if (i > 100 && i % 5 == 1)
            {
                SInfo newsi = new SInfo();
                newsi.Name = $"实例{i + 10}";
                si.Manager.DeleteNode(newsi);
                si.Log(si.Manager.GetThreadsInfo());
            }
        }

        public void StateInit(SInfo si)
        {

            //var cfg = si.GetConfig<M1Cfg>();
            //si.Log(cfg.Test.ToString());
        }

        public void StateUInit(SInfo si)
        {

        }
    }
    public class SMTestModel2 : IState
    {
        public void StateHandle(SInfo si)
        {
            int i = (int?)si.GetUserData("i") ?? 0;
            si.Log($"测试{i}");
            si.SetUserData("i", i + 1);
            si.SetNextState("1", 200);
            //if (i == 3)
            //    si.Manager.Paused = true;
        }

        public void StateInit(SInfo si)
        {
        }

        public void StateUInit(SInfo si)
        {

        }
    }
}
